package com.cyt.web.controller.sys;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysAppModule;
import com.cyt.sys.model.vo.SysAppModuleVo;
import com.cyt.sys.service.SysAppMenuService;
import com.cyt.sys.service.SysAppModuleService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用控制层
 *
 * @author dingxu
 * @date 2019-04-03 10:00
 */
@RestController
public class SysAppModuleController extends SysBaseController {

    @Resource
    private SysAppModuleService sysAppModuleService;
    @Resource
    private SysAppMenuService sysAppMenuService;

    /**
     * 系统模块统一跳转处理
     *
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @RequestMapping("/sys/baseAppModule/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 应用管理分页
     *
     * @param page:当前页面
     * @param limit:页面大小
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "分页查询", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/page", method = RequestMethod.POST)
    public ReturnResult page(SysAppModuleVo sysAppModuleVo, int limit, int page) {
        ReturnResult returnResult = sysAppModuleService.page(sysAppModuleVo, limit, page);
        return returnResult;
    }


    /**
     * 跳转保存/更新页面
     *
     * @param id 应用主键
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @RequestMapping(value = "/sysAppModule/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/sys/baseAppModule/baseAppModuleEdit");
        if (id != null && !"".equals(id)) {
            SysAppModule sysAppModule = sysAppModuleService.selectByPrimaryKey(id);
            view.addObject(sysAppModule);
            List<String> list = sysAppMenuService.selectMenuIdByAppId(id);
            if (list != null && list.size() > 0) {
                String s = "";
                for (String t : list) {
                    if (StringUtils.isEmpty(s)) {
                        s = t;
                    } else {
                        s += "," + t;
                    }
                }
                sysAppModule.setMenuIds(s);
            }

        }
        return view;
    }

    /**
     * 跳转详情页面
     *
     * @param id 应用主键
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @RequestMapping(value = "/sysAppModule/getView", method = RequestMethod.GET)
    public ModelAndView getView(String id) {
        ModelAndView view = new ModelAndView("/sys/baseAppModule/baseAppModuleView");
        if (id != null && !"".equals(id)) {
            SysAppModule sysAppModule = sysAppModuleService.selectByPrimaryKey(id);
            view.addObject(sysAppModule);
            List<String> list = sysAppMenuService.selectMenuIdByAppId(id);
            if (list != null && list.size() > 0) {
                String s = "";
                for (String t : list) {
                    if (StringUtils.isEmpty(s)) {
                        s = t;
                    } else {
                        s += "," + t;
                    }
                }
                sysAppModule.setMenuIds(s);
            }

        }
        return view;
    }

    /**
     * 保存/更新字典数据
     *
     * @param sysAppModuleVo 对象
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "保存/更新应用", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(SysAppModuleVo sysAppModuleVo) {
        ReturnResult result = sysAppModuleService.addOrUpdate(sysAppModuleVo);
        return result;
    }

    /**
     * 删除字典数据
     *
     * @param id 主键id数组字符串集
     * @author dingxu
     * @date 2019-04-03 10:00
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "删除应用", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/delete", method = RequestMethod.GET)
    public ReturnResult delete(String id) {
        return sysAppModuleService.delete(id);
    }

    /**
     * 生成密钥对
     *
     * @param id 主键id数组字符串集
     * @author dingxu
     * @date 2019-04-09 10:00
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "生成密钥对", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/sendKey", method = RequestMethod.GET)
    public ReturnResult sendKey(String id) throws Exception {
        return sysAppModuleService.sendKey(id);
    }

    /**
     * 菜单加密
     *
     * @param id 主键id数组字符串集
     * @author dingxu
     * @date 2019-04-09 10:00
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "菜单加密", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/encordBassMenu", method = RequestMethod.GET)
    public ReturnResult encordBassMenu(String id) throws Exception {

        return sysAppModuleService.encordBassMenu(id);
    }

    /**
     * 获取应该模块信息
     *
     * @param status
     * @return
     * @author sunli
     */
    @BaseLog(module = "系统模块", classz = "应用管理", methods = "菜单树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysAppModule/getApplyList", method = RequestMethod.POST)
    public ReturnResult getAllDate(String status) {
        ReturnResult returnResult = sysAppModuleService.getAllApply(status);
        return returnResult;
    }


}
